package com.second.app.spring.annoations.autowired;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.util.ReflectionUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author GYZ
 * @Date 2023/9/14
 * <pre>
 *
 * </pre>
 */
//public class ApplicationContextDemo {
//
//    public static void main(String[] args) throws Exception {
//        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.second.app.spring.annoations.autowired");
//        StudentComponent studentComponent = (StudentComponent) ctx.getBean("studentComponent");
//        System.out.println(studentComponent);
//
//
//        List<Class<? extends Annotation> > autowiredAnnotationTypes = new ArrayList<>();
//
//        autowiredAnnotationTypes.add(Autowired.class);
//
//        ReflectionUtils.doWithLocalMethods(StudentComponent.class,method -> {
//            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
//            if (bridgedMethod.getAnnotations().length > 0) {  // autowiring annotations have to be local
//                for (Class<? extends Annotation> type : autowiredAnnotationTypes) {
//                    //todo 从字段上获取注解@Autowired 或注解 @Value
//                    AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(bridgedMethod, type);
//                    //todo 如果注解存在
//                    if (attributes != null) {
//                        for (Map.Entry<String, Object> stringObjectEntry : attributes.entrySet()) {
//                            System.out.println("key:"+stringObjectEntry.getKey()+", value:"+stringObjectEntry.getValue());
//                        }
//                    }
//                }
//            }
//        });
//    }
//}
